home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
6_2.lha
/
6_2
/
6_2a.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
676b
|
32 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ substring operator
/ exercise 6.2
tring string::operator()(int index, int count)
char *s1 = p->s;
int s1len = strlen(s1);
// convert left index, if necessary
if (index < 0)
index += s1len;
// left index past end of string
else if (index >= s1len)
return "";
// convert count, if necessary
int numleft = s1len - index;
if (count > numleft || count < 0)
count = numleft;
// copy the substring
char *s2 = new char[count + 1];
strncpy(s2, s1+index, count);
s2[count] = '\0';
return s2;